﻿using GN.Pay.TradeChannel.Gyb.Response;
using GN.Pay.TradeChannel.ProtocolPay;
using GN.Pay.Utils;
using GN.Pay.Utils.Net.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GN.Pay.TradeChannel.Gyb.Request
{
    [Serializable]
    class ProtocolUnBindRequest : ARequest
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="channel"></param>
        /// <param name="apply"></param>
        /// <param name="config"></param>
        public ProtocolUnBindRequest(IChannel channel,
           IProtocolUnBindApply apply,
           GybConfigure config) : base(config)
        {
            if (apply == null)
            {
                channel.ThrowChannelValidationException("请求的 apply 为 null");
            }
            if (string.IsNullOrWhiteSpace(apply.ProtocolNo))
            {
                channel.ThrowChannelValidationException("协议号为空");
            }
            ProtocolNo = apply.ProtocolNo;
        }

        /// <summary>
        /// 
        /// </summary>
        public override string TranAbbr
        {
            get
            {
                return TradeCommand.XYJY;
            }
        }


        /// <summary>
        /// 终端号,可以全为0
        /// </summary>
        public string ProtocolNo { get; set; }

        /// <summary>
        /// 生成
        /// </summary>
        /// <param name="channel">通道</param>
        /// <param name="config">配置</param>
        /// <returns></returns>
        public ProtocolUnBindResponse Build(ITradeChannel channel,
            GybConfigure config)
        {
            var plain = BuildPlain();
            var dic = new SignDictionary();
            dic["transName"] = this.TranAbbr;
            dic["Plain"] = plain;
            dic["Signature"] = SecurityUtils.Sign(config.ConfigureInfo, plain);

            string requestContent = BuildXML(dic, config);

            HttpClient client = new HttpClient();
            client.Encoding = config.Encoding;
            client.ContentType = "text/xml";
            var response = client.PostString(new Uri(config.GatewayUrl), requestContent);

            return new ProtocolUnBindResponse(response, channel, config);
        }
    }
}
